import 'dart:async';

import 'package:flutter/material.dart';
import 'package:gold/base/view_model.dart';
import 'package:gold/http/api_login.dart';
import 'package:gold/http/api_order.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/user_list_model.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/xfg_extension.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

/// @description: 授权提货页面
/// @author tangzhentao
/// @created at 22:49 2025/2/27

class AuthorizationCollectViewmodel extends Viewmodel {
  /// 订单id
  final int _orderId;

  /// 刷新控制器
  final RefreshController controller = RefreshController();

  /// 数据列表
  final List<SearchUserModel> userList = [];

  int? selectIndex;

  /// 搜索控制器
  final TextEditingController editingController = TextEditingController();
  final FocusNode foucsNode = FocusNode();

  /// 页码
  int _page = 1;

  AuthorizationCollectViewmodel(this._orderId);

  /// 确认授权
  Future confirmAuthorization(SearchUserModel user) async {
    final result = await Http().request<dynamic>(ApiOrder.setPickupUser, loadingText: '授权代取中...', successText: '已授权', data: {
      'order_id': _orderId, 'pickup_user_id': user.uid,
    });
    if (result.success) {
      XfgNav.pop(true);
    }
  }

  /// 受让人变化
  void changeSelect(int index) {
    selectIndex = index;
    setSuccessChange();
  }

  /// 搜索数据
  Future search() async {
    if (editingController.text.isEmpty) {
      '请输入手机号/姓名'.info();
      return;
    }
    selectIndex = null;
    _page = 1;
    setLoading();
    final result = await Http().request<UserListModel>(ApiLogin.searchUser,
        data: {'page': _page, 'keywords': editingController.text}
    );
    if (result.success) {
      userList.clear();
      userList.addAll(result.data?.data?? []);
      if (userList.isNotEmpty) {
        setSuccess();
        controller.headerMode?.value = RefreshStatus.canRefresh;
        controller.footerMode?.value = _page < (result.data?.totalPages?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      } else {
        setEmpty();
      }
    } else {
      setFailed(msg: result.msg);
    }
  }

  /// 刷新数据
  Future refresh() async {
    _page = 1;
    final result = await Http().request<UserListModel>(ApiLogin.searchUser,
        data: {'page': _page, 'keywords': editingController.text}
    );
    if (result.success) {
      controller.refreshCompleted();
      controller.footerMode?.value = _page < (result.data?.totalPages?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.data?? [];
      if (data.isNotEmpty) {
        userList.clear();
        userList.addAll(data);
        setSuccessChange();
      }
    } else {
      controller.refreshFailed();
    }
  }

  /// 加载更多数据
  Future loadMore() async {
    _page++;
    final result = await Http().request<UserListModel>(ApiLogin.searchUser,
        data: {'page': _page, 'keywords': editingController.text}
    );
    if (result.success) {
      controller.loadComplete();
      controller.footerMode?.value = _page < (result.data?.totalPages?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.data?? [];
      if (data.isNotEmpty) {
        userList.addAll(data);
        setSuccessChange();
      } else {
        controller.loadNoData();
      }
    } else {
      controller.loadFailed();
    }
  }

  @override
  onReload() => search();

  @override
  onRetry() => search();

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}
